package com.wu.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.wu.dao.UserDao;
import com.wu.entity.User;
import com.wu.receiver.UserPayReceiver;
import com.wu.util.BeanUtil;
import com.wu.util.PagedResult;

/**
 * 功能概要：UserService实现类
 * @author 杭州研发-吴冬冬
 * @date 2016年12月15日
 */
@Service
@Repository
public class UserServiceImpl implements UserService, ApplicationContextAware{
	private static final Logger log = Logger.getLogger(UserServiceImpl.class);
	@Resource
	private UserDao userDao;
	private ApplicationContext context;
	public User selectUserById(Integer userId) {
		return userDao.selectUserById(userId);
		
	}
	
	@Transactional
	public List<User> selectUserByUserName(String userName) {
		List<User> list = userDao.selectUserByUserName(userName);
		//触发event
		log.info("---------触发event！---------");
		System.out.println("---------触发event！---------");
		context.publishEvent(new UserPayReceiver(this,"1","哈哈",100));
		return list;
	}

	public PagedResult<User> queryByPage(String userName,Integer pageNo,Integer pageSize ) {
		pageNo = pageNo == null?1:pageNo;
		pageSize = pageSize == null?10:pageSize;
		PageHelper.startPage(pageNo,pageSize);  //startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		return BeanUtil.toPagedResult(userDao.selectUserByUserName(userName));
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		 log.debug("Autowired applicationContext");
		 this.context = applicationContext;
		
	}

}
